﻿using System.Collections.Generic;
using System.Linq;

namespace Sidvall.Office
{
	public class WorksheetCollection : IWorksheetCollection
	{
		#region Public Members

		#region Count

		public int Count
		{
			get
			{
				return this.Workbook.InternalWorkbook.Worksheets.Count;
			}
		}

		#endregion
		#region this

		public IWorksheet this[string name]
		{
			get
			{
				var item = this.Workbook.InternalWorkbook.Worksheets[name];
				return new Worksheet(item);
			}
		}
		public IWorksheet this[int index]
		{
			get
			{
				var item = this.Workbook.InternalWorkbook.Worksheets[index];
				return new Worksheet(item);
			}
		}

		#endregion

		#region Add

		public Worksheet Add(string name)
		{
			var item = this.Workbook.InternalWorkbook.Worksheets.Add(name);
			return new Worksheet(item);
		}

		#endregion
		#region Clear

		public void Clear()
		{
			this.Workbook.InternalWorkbook.Worksheets.Clear();
		}

		#endregion
		#region Contains

		public bool Contains(IWorksheet item)
		{
			var value = item as Worksheet;
			if (value == null)
				return false;
			return this.Workbook.InternalWorkbook.Worksheets.Contains(value.InternalWorksheet);
		}

		#endregion
		#region Exists

		public bool Exists(string name)
		{
			return this.Workbook.InternalWorkbook.Worksheets.Exists(name);
		}

		#endregion
		#region IndexOf

		public int IndexOf(IWorksheet item)
		{
			var value = item as Worksheet;
			if (value == null)
				return -1;
			return this.Workbook.InternalWorkbook.Worksheets.IndexOf(value.InternalWorksheet);
		}

		#endregion
		#region Remove

		public bool Remove(string name)
		{
			var item = (from o in this.Workbook.InternalWorkbook.Worksheets
						where o.Name.Equals(name, System.StringComparison.OrdinalIgnoreCase)
						select o).FirstOrDefault();
			if (item == null)
				return false;
			return this.Workbook.InternalWorkbook.Worksheets.Remove(item);
		}

		#endregion
		#region RemoveAt

		public void RemoveAt(int index)
		{
			this.Workbook.InternalWorkbook.Worksheets.RemoveAt(index);
		}

		#endregion
		#region ToString

		public override string ToString()
		{
			return "WorksheetCollection - Sidvall.Office.IG";
		}

		#endregion

		#region IWorksheetCollection Members

		IWorksheet IWorksheetCollection.Add(string name)
		{
			return Add(name);
		}
		IEnumerator<IWorksheet> IEnumerable<IWorksheet>.GetEnumerator()
		{
			foreach (var item in this.Workbook.InternalWorkbook.Worksheets)
				yield return new Worksheet(item);
		}
		System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
		{
			foreach (var item in this.Workbook.InternalWorkbook.Worksheets)
				yield return new Worksheet(item);
		}

		#endregion

		#endregion
		#region Private Members

		internal Workbook Workbook { get; set; }

		#endregion
		#region Constructors

		public WorksheetCollection(Workbook workbook)
		{
			if (workbook == null)
				throw new System.ArgumentNullException("workbook");
			this.Workbook = workbook;
		}

		#endregion
	}
}
